package com.itheima.advantagesup.service.impl;

import com.itheima.advantagesup.entity.SysMenu;
import com.itheima.advantagesup.mapper.SysMenuMapper;
import com.itheima.advantagesup.service.SysMenuService;
import com.itheima.advantagesup.vo.MenuVO;
import com.itheima.advantagesup.vo.MenuVO2;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SysMenuServiceImpl implements SysMenuService {

    private final SysMenuMapper sysMenuMapper;
    /**
     * /sys_menu/allData 查询所有菜单
     * @return
     */
    @Override
    public List<SysMenu> getAllData() {
        //先拿到最高级父类的所有数据
        List<SysMenu> sysMenuList = sysMenuMapper.getAllDataByPId(0);
        sysMenuList.forEach(sysMenu->{
            //sysMenuList的子类充当父类,再拿去该数据
            List<SysMenu> sysMenuList1 = sysMenuMapper.getAllDataByPId(sysMenu.getId());
            sysMenuList1.forEach(sysMenu1->{
                //sysMenuMapper.getAllDataByPId(sysMenu1.getId())
                sysMenu1.setChildren(sysMenuMapper.getAllDataByPId(sysMenu1.getId()));
            });
        sysMenu.setChildren(sysMenuList1);
        });
        return sysMenuList;
    }

    @Override
    public void addMenu(MenuVO menuVO) {

        menuVO.setCreateDate(LocalDateTime.now());
        menuVO.setUpdateDate(LocalDateTime.now());
        menuVO.setDel_flag(0);
        //menuVO.setDefaultData("1");
        sysMenuMapper.addMenu(menuVO);
    }

    /**
     * 根据id删除菜单
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        sysMenuMapper.deleteById(id);
    }

    /**
     * 修改菜单
     * @param menuVO
     */
    @Override
    public void updateMenu(MenuVO menuVO) {
        menuVO.setUpdateDate(LocalDateTime.now());
        sysMenuMapper.updateMenu(menuVO);
    }

    @Override
    public MenuVO2 rowInfo(Integer id) {
        MenuVO2 menuVo2 = sysMenuMapper.rowInfo(id);
        return menuVo2;
    }
}
